package gonz.grabber.modernbag;

import gonz.common.config.GrabLink;
import gonz.grabber.Grabber;
import gonz.grabber.Item;
import gonz.grabber.ItemProperty;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class VScollection extends Grabber {
    public static final String LINKS_XPATH = "//*[@id=\"collection-set\"]/ul[2]/li/a[2]";
    public static final String[] IMG_LINKS_XPATHS = new String[]
            {"//*[@id=\"productForm\"]/section[1]/ul/li[2]/fieldset[1]/section[1]/div/span/a/label/span",
                    "//*[@id=\"productForm\"]/section[1]/ul/li[2]/fieldset[1]/section[2]/div/span/a/label/span",
                    "//*[@id=\"productForm\"]/section[1]/ul/li[2]/fieldset[1]/section[3]/div/span/a/label/span"};
    //*[@id="productForm"]/   section[1]/ul/li[2]/fieldset[1]/section[2]/div/span/a[4]/label/span
    //*[@id="productForm"]/   section[1]/ul/li[2]/fieldset[1]/section[2]/div/span/a[5]/label/span
    //*[@id="productForm"]  /section[1]/ul/li[2]/fieldset[1]/section[2]/div/span/a[4]/label/span
    //*[@id="productForm"]/section[1]/ul/li[2]/fieldset[1]/section[2]/div/span/a[3]/label/span
    public static final String IMG_BASE_URL = "http://dm.victoriassecret.com/product/760x1013/";
    public static final String IMG_EXT = ".jpg";
    public static final String MAIN_IMG_XPATH = "//*[@id=\"vsImage\"]";
    public static final String ITEM_ID = "//*[@id=\"productForm\"]/section[1]/ul/li[1]/section/div/div[3]/div";
    private static final Logger LOGGER = Logger.getLogger(VScollection.class);

    @Override
    protected List<Item> grabItems(WebDriver webDriver, GrabLink grabLink) {
        List<Item> items = new ArrayList<Item>();
        String linkToGrab = grabLink.getLink();
        webDriver.get(linkToGrab);

        List<WebElement> itemElements = webDriver.findElements(By.xpath(LINKS_XPATH));
        LOGGER.info("Found " + itemElements.size() + " items on page " + linkToGrab);
        for (int i = 0; i < itemElements.size(); i++) {
            WebElement itemElement = itemElements.get(i);
            String itemLink = itemElement.getAttribute("href");
            String itemName = getItemName(itemElement);
            Properties properties = new Properties();
            properties.setProperty(ItemProperty.REFERER.toString(), itemLink);
            Item item = new Item(String.valueOf(i), itemName, itemLink, properties);
            items.add(item);
        }
        for (Item item : items) {
            try {
                webDriver.get(item.getLink());
                //WebElement itemIdElement = webDriver.findElement(By.xpath(ITEM_ID));
                //String itemId = itemIdElement.getText();
//                if (StringUtils.isNotEmpty(itemId)) {
//                    item.setName(itemId + item.getName());
//                } else {
//                    LOGGER.error("Item id is empty. " + item);
//                }
                List<String> imgLinks = new ArrayList<String>();

                for (String imgLinksXpath : IMG_LINKS_XPATHS) {
                    List<WebElement> imgLinkSpanElements = webDriver.findElements(By.xpath(imgLinksXpath));
                    for (WebElement imgLinkSpanElement : imgLinkSpanElements) {
                        String imgName = imgLinkSpanElement.getAttribute("data-alt-image");
                        if (StringUtils.isNotEmpty(imgName)) {
                            imgLinks.add(IMG_BASE_URL + imgName + IMG_EXT);
                        }
                    }
                    if (!imgLinks.isEmpty()) {
                        break;
                    }
                }

                if (imgLinks.isEmpty()) {
                    LOGGER.warn("Image links not found in colors sections. " + item);
                    WebElement mainImgElement = webDriver.findElement(By.xpath(MAIN_IMG_XPATH));
                    String mainImgLink = mainImgElement.getAttribute("data-zoom-src");
                    if (StringUtils.isNotEmpty(mainImgLink)) {
                        imgLinks.add("http:" + mainImgLink);
                    } else {
                        LOGGER.error("Main image link not found. " + item);
                    }
                }
                item.setImgLinks(imgLinks);
            } catch (RuntimeException e) {
                LOGGER.error("RuntimeException on during grab item: " + item, e);
                throw e;
            }
        }
        return items;
    }

    private String getItemName(WebElement itemElement) {
        String name = "";
        List<WebElement> nameElements = itemElement.findElements(By.xpath("aside/hgroup/*"));
        for (int i = 0; nameElements != null && i < nameElements.size(); i++) {
            WebElement nameElement = nameElements.get(i);
            name += nameElement.getText();
        }
        nameElements = itemElement.findElements(By.xpath("aside/p"));
        for (int i = 0; nameElements != null && i < nameElements.size(); i++) {
            WebElement nameElement = nameElements.get(i);
            name += " " + nameElement.getText();
        }
        if (StringUtils.isEmpty(name)) {
            name = "Undefined";
        }
        return name;

    }
}
